<?php
	
	require_once("createUserHandler.php");
	require_once("createUserView.php");
	require_once("Test/FormValidator.php");
	
	class CreateUserController
	{		
		public function DoControll(DatabaseHandler $database)
		{
			$createUserHandler = new CreateUserHandler($database);
			$createUserView = new CreateUserView();
			$loginHandler = new LoginHandler($database);
			
			$formValidator = new FormValidator();
			
			if($loginHandler->IsLoggedIn() == false)
			{
				$return = $createUserView->RegisterMessage(CreateUserView::REGISTER_HEADER) . $createUserView->DoRegisterBox();
				
				if($createUserView->TriedToRegister() == true)
				{
					$errors = $createUserHandler->VerifyUser($createUserView->GetRegisterUserName(), $createUserView->GetRegisterPassword1(), $createUserView->GetRegisterPassword2());
					
					if(count($errors) == 0)
					{
						if($createUserHandler->UserExists($createUserView->GetRegisterUserName(), $createUserView->GetRegisterPassword1()) == false)
						{
							$createUserHandler->CreateUser($createUserView->GetRegisterUserName(), $createUserView->GetRegisterPassword1());
							$return .= $createUserView->RegisterMessage($createUserView::USER_ADDED);
						}
						else 
						{
							$return .= $createUserView->RegisterMessage($createUserView::USER_EXISTS);
						}
					}
					else if (count($errors) == 1)
					{
						$return .= $errors;
					}
					else 
					{
						foreach($errors as $error)
						{
							$return .= $error . "<br/>";
						}
					}
				}
				
				return $return;
			}
		}
	}
?>